package com.huige.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

/**
 * ClassName: DemoFilter
 * Package: com.huige.filter
 * Description:
 *
 * @Author 朱志辉
 * @Create 2024/5/6 20:42
 * @Version 1.0
 */
//@WebFilter(urlPatterns = "/*")
public class DemoFilter implements Filter {

    @Override//初始化方法，只调用一次
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init 初始化方法执行了");
    }

    @Override//拦截请求的方法，可以调用多次
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //放行前的逻辑
        System.out.println("Demo 拦截到请求.....放行前的逻辑");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
        //放行后的逻辑
        System.out.println("Demo 拦截到响应.....放行后的逻辑");
    }

    @Override//销毁的方法，只调用一次
    public void destroy() {
        System.out.println("destroy 销毁方法执行了");
    }
}
